package org.orbis.delete;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.orbis.communicate.Configuration;
import org.orbis.communicate.MainService;
import org.orbis.communicate.deleter.DeleterPackage;
import org.orbis.tool.Header;

public class DeleteServer extends HttpServlet{
	private static final long serialVersionUID = 2257377838098796062L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException{
		MainService postService = MainService.getInstance();
		Configuration conf = postService.getConf();

		if(req.getParameterMap().size() == 0){
			resp.setCharacterEncoding("UTF-8");
			PrintWriter out = resp.getWriter();

			out.println("<!DOCTYPE html>");
			out.println("<html>");
			out.println("<head>");
			out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
			out.println("<title>ORBIS Search System: Delete</title>");
			out.println("</head>");
			out.println("<body>");
			// Title bar
			out.println(Header.getTitleBar("ORBIS Search System: Delete"));

			// Header
			out.println(Header.getHeader(3));

			out.println("<div>");
			out.println("<form method=\"GET\" action=\"delete\">");
			out.println("Delete type: ");
			out.println("<select name=\"type\">");
			String contentType = conf.contentInfo().contentName(conf.contentDeleteType());
			out.println("<option value=\"content\">Content (" + contentType + ")</option>");
			String appendixType = conf.contentInfo().appendixName(conf.appendixDeleteType());
			out.println("<option value=\"appendix\">Appendix (" + appendixType + ")</option>");
			out.println("<option value=\"ID\">Internal ID</option>");
			int flagSize = conf.contentInfo().integerFlagNum();
			for(int i = 0; i < flagSize; i++){
				String name = conf.contentInfo().integerFlagName(i);
				out.println("<option value=\"" + name + "\">" + name + "</option>");
			}
			flagSize = conf.contentInfo().longFlagNum();
			for(int i = 0; i < flagSize; i++){
				String name = conf.contentInfo().longFlagName(i);
				out.println("<option value=\"" + name + "\">" + name + "</option>");
			}
			out.println("</select>");
			out.println("</div>");
			out.println("<div>");
			out.println("<label>Key: <textarea col=10 row=200 name=\"key\"></textarea></label>");
			out.println("</div>");

			out.println("<input type=\"submit\" value=\"Delete\">");
			out.println("<input type=\"reset\" value=\"Reset\">");

			out.println("</form>");
			out.println("</body>");
			out.println("</html>");

			return;
		}

		String type = req.getParameter("type");
		String key = req.getParameter("key");

		if(type.equals("content")) postService.postDelete(new DeleterPackage(true, key));
		else if(type.equals("appendix")) postService.postDelete(new DeleterPackage(true, key));
		else{
			int flagSize = conf.contentInfo().integerFlagNum();
			for(int i = 0; i < flagSize; i++){
				String name = conf.contentInfo().integerFlagName(i);
				if(!type.equals(name)) continue;
				try{
					int value = Integer.parseInt(key);
					postService.postDelete(new DeleterPackage(i, value));
				}catch(NumberFormatException e){
					continue;
				}
			}
			flagSize = conf.contentInfo().longFlagNum();
			for(int i = 0; i < flagSize; i++){
				String name = conf.contentInfo().longFlagName(i);
				if(!type.equals(name)) continue;
				try{
					long value = Integer.parseInt(key);
					postService.postDelete(new DeleterPackage(i, value));
				}catch(NumberFormatException e){
					continue;
				}
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException{
		MainService postService = MainService.getInstance();
		Configuration conf = postService.getConf();

		String type = req.getParameter("type");
		String key = req.getParameter("key");

		if(type.equals("content")) postService.postDelete(new DeleterPackage(true, key));
		else if(type.equals("appendix")) postService.postDelete(new DeleterPackage(true, key));
		else{
			int flagSize = conf.contentInfo().integerFlagNum();
			for(int i = 0; i < flagSize; i++){
				String name = conf.contentInfo().integerFlagName(i);
				if(!type.equals(name)) continue;
				try{
					int value = Integer.parseInt(key);
					postService.postDelete(new DeleterPackage(i, value));
				}catch(NumberFormatException e){
					continue;
				}
			}
			flagSize = conf.contentInfo().longFlagNum();
			for(int i = 0; i < flagSize; i++){
				String name = conf.contentInfo().longFlagName(i);
				if(!type.equals(name)) continue;
				try{
					long value = Integer.parseInt(key);
					postService.postDelete(new DeleterPackage(i, value));
				}catch(NumberFormatException e){
					continue;
				}
			}
		}
	}
}
